package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.mapper.EmployeeMapper;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IEmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Service
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    public void save(Employee employee, Long[] ids) {
        //为密码加密
        String password = employee.getPassword();
        //加盐
        Md5Hash md5Hash = new Md5Hash(password, employee.getName());
        employee.setPassword(md5Hash.toString());

        employeeMapper.insert(employee);
        //往员工和角色中间表中添加数据
        if(ids != null && ids.length >0){
            for (Long id : ids) {
                employeeMapper.insertToEmp_Role(employee.getId(),id);
            }
        }

    }

    @Override
    public void delete(Long id) {
        employeeMapper.deleteByPrimaryKey(id);
        employeeMapper.deleteByEmployeeId(id);
    }

    @Override
    public void update(Employee employee, Long[] ids) {
        employeeMapper.updateByPrimaryKey(employee);

        //往员工和角色中间表中更新数据
        //防止重复需要将之前旧数据先删掉
        employeeMapper.deleteByEmployeeId(employee.getId());
        if(ids != null && ids.length >0){
            for (Long id : ids) {
                employeeMapper.insertToEmp_Role(employee.getId(),id);
            }
        }

    }

    @Override
    public Employee get(Long id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Employee> list() {
        return employeeMapper.selectAll();
    }

    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Employee> list = employeeMapper.selectForList(qo);
        return new PageInfo(list);
    }

    @Override
    public Employee getByUsernameAndPassword(String username, String password) {

        return employeeMapper.selectByNameAndPassword(username, password);
    }

    @Override
    public List<String> selectByEmployeeId(Long id) {

        return employeeMapper.selectByEmployeeId(id);
    }

    @Override
    public void deleteBatch(Long[] ids) {
        employeeMapper.deleteByIds(ids);
        employeeMapper.deleteMidTableByIds(ids);
    }

    @Override
    public Employee selectByName(String name) {
        return employeeMapper.selectByName(name);
    }

    @Override
    public Workbook exportXls() {
        //创建excel文件
        Workbook wb = new HSSFWorkbook();
        //创建页
        Sheet sheet = wb.createSheet("员工名单");
        //创建行 第一行是标题
        Row row = sheet.createRow(0);
        row.createCell(0).setCellValue("姓名");
        row.createCell(1).setCellValue("邮箱");
        row.createCell(2).setCellValue("年龄");
        //查询员工数据
        List<Employee> employees = employeeMapper.selectAll();
        for (int i = 0; i < employees.size(); i++) {
            //每个员工一行,从第二行开始
            Employee employee = employees.get(i);
            row = sheet.createRow(i+1);
            row.createCell(0).setCellValue(employee.getName());
            row.createCell(1).setCellValue(employee.getEmail());
            row.createCell(2).setCellValue(employee.getAge());
        }
        return wb;
    }

    public void importXls(MultipartFile file) throws Exception {
        //读取接收到的文件
        //获取输入流
        HSSFWorkbook wb = new HSSFWorkbook(file.getInputStream());
        //读取哪一页
        HSSFSheet sheet = wb.getSheetAt(0);
        //获取行的最大索引数
        int rows = sheet.getLastRowNum();
        //第一行不用读,从1开始
        for (int i = 1; i <= rows; i++) {
            //获取每一行
            Row row = sheet.getRow(i);
            Employee employee = new Employee();
            //把每一行的数据设置给员工
            employee.setName(row.getCell(0).getStringCellValue());
            employee.setEmail(row.getCell(1).getStringCellValue());
            //表格里数值类型的
            double age = row.getCell(2).getNumericCellValue();
            employee.setAge((int)age);
            //设置一个默认密码
            employee.setPassword("1");
            //保存到数据库
            this.save(employee,null);
        }
    }

    @Override
    public List<Employee> selectByRole(String... sns) {
        return employeeMapper.selectByRole(sns);
    }


}
